package com.rrd.dw.mr.gzip.kafka;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.tianjiscore.TianjiScoreV3Data;
import com.ucredit.babel.common.model.tianjiscore.TianjiScoreV6Record;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * 融360分期分的v6互金增强版接口
 *
 * @author renzhanzheng
 */

public class TianjiScoreV6Parse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 融360分期分的v6互金增强版接口
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     * @return
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {

        TianjiScoreV6Record obj = (TianjiScoreV6Record)model;
        TianjiScoreV3Data data = obj.getData();
        JSONObject info =data.getData() == null ? new JSONObject() : JSON.parseObject(data.getData()) ;
        JSONObject tianji = info.getJSONObject("tianji_api_fenqisolution_scorev6highpro_response") == null ? new JSONObject() : info.getJSONObject("tianji_api_fenqisolution_scorev6highpro_response");

        StringBuilder sb = getBaseInfo(obj, successful);
        //状态码
        sb.append(info.getString("error") == null ? "" : info.getString("error")).append(CRT001);
        //请求ID
        sb.append(info.getString("request_id" ) == null ? "" : info.getString("request_id" )).append(CRT001);
        //分期模型分
        sb.append(tianji.getString("score") == null ? "" : tianji.getString("score")).append(CRT001);
        //模型版本
        sb.append(tianji.getString("modelId") == null ? "" : tianji.getString("modelId")).append(CRT001);

        key.set(getKey());
        val.set(sb.toString().replace("\n", " ").replace("\r", " "));
        context.write(key, val);
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_tianji_fenqiscorev6_inc";
    }

    @Override
    public String getType() {
        return RecordType.TIANJI_SCOREV6.toString();
    }

    @Override
    public String getComment() {
        return "融360分期分的v6互金增强版接口";
    }
}
